package com.imut.lagain.repository;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imut.lagain.entity.CareTask;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 关怀任务数据访问层
 */
@Mapper
public interface CareTaskRepository extends BaseMapper<CareTask> {

    /**
     * 根据用户ID查询待完成任务
     * @param userId 用户ID
     * @return 待完成任务列表
     */
    @Select("SELECT * FROM care_task WHERE user_id = #{userId} AND status = 'pending' AND is_deleted = 0 ORDER BY priority DESC, created_at ASC")
    List<CareTask> findPendingTasksByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID和生理期阶段查询任务
     * @param userId 用户ID
     * @param cyclePhase 生理期阶段
     * @return 任务列表
     */
    @Select("SELECT * FROM care_task WHERE user_id = #{userId} AND cycle_phase = #{cyclePhase} AND status = 'pending' AND is_deleted = 0 ORDER BY priority DESC")
    List<CareTask> findByUserIdAndCyclePhase(@Param("userId") Long userId, @Param("cyclePhase") String cyclePhase);

    /**
     * 根据任务ID查询任务
     * @param taskId 任务ID
     * @return 任务
     */
    @Select("SELECT * FROM care_task WHERE task_id = #{taskId} AND is_deleted = 0")
    CareTask findByTaskId(@Param("taskId") String taskId);

    /**
     * 完成任务
     * @param taskId 任务ID
     * @param completedBy 完成者ID
     * @param completedAt 完成时间
     * @return 更新行数
     */
    @Update("UPDATE care_task SET status = 'completed', completed_by = #{completedBy}, completed_at = #{completedAt}, updated_at = NOW() WHERE task_id = #{taskId} AND is_deleted = 0")
    int completeTask(@Param("taskId") String taskId, @Param("completedBy") Long completedBy, @Param("completedAt") LocalDateTime completedAt);

    /**
     * 根据用户ID和关怀类型查询任务
     * @param userId 用户ID
     * @param careType 关怀类型
     * @return 任务列表
     */
    @Select("SELECT * FROM care_task WHERE user_id = #{userId} AND care_type = #{careType} AND is_deleted = 0 ORDER BY created_at DESC")
    List<CareTask> findByUserIdAndCareType(@Param("userId") Long userId, @Param("careType") String careType);

    /**
     * 查询用户在指定时间范围内的任务
     * @param userId 用户ID
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return 任务列表
     */
    @Select("SELECT * FROM care_task WHERE user_id = #{userId} AND created_at BETWEEN #{startDate} AND #{endDate} AND is_deleted = 0 ORDER BY created_at DESC")
    List<CareTask> findByUserIdAndDateRange(
            @Param("userId") Long userId, 
            @Param("startDate") LocalDateTime startDate, 
            @Param("endDate") LocalDateTime endDate);

    /**
     * 查询过期的待完成任务
     * @param currentTime 当前时间
     * @return 过期任务列表
     */
    @Select("SELECT * FROM care_task WHERE status = 'pending' AND due_date < #{currentTime} AND is_deleted = 0")
    List<CareTask> findOverdueTasks(@Param("currentTime") LocalDateTime currentTime);

    /**
     * 统计用户任务完成情况
     * @param userId 用户ID
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return 完成任务数量
     */
    @Select("SELECT COUNT(*) FROM care_task WHERE user_id = #{userId} AND status = 'completed' AND completed_at BETWEEN #{startDate} AND #{endDate} AND is_deleted = 0")
    int countCompletedTasks(
            @Param("userId") Long userId, 
            @Param("startDate") LocalDateTime startDate, 
            @Param("endDate") LocalDateTime endDate);

    /**
     * 统计用户总任务数量
     * @param userId 用户ID
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return 总任务数量
     */
    @Select("SELECT COUNT(*) FROM care_task WHERE user_id = #{userId} AND created_at BETWEEN #{startDate} AND #{endDate} AND is_deleted = 0")
    int countTotalTasks(
            @Param("userId") Long userId, 
            @Param("startDate") LocalDateTime startDate, 
            @Param("endDate") LocalDateTime endDate);
}
